package com.huizu.project.controller;

import java.rmi.ServerException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollUtil;
import com.huizu.common.core.domain.entity.SysUser;
import com.huizu.common.exception.ServiceException;
import com.huizu.common.utils.SecurityUtils;
import com.huizu.project.domain.ProBj;
import com.huizu.project.domain.ProXx;
import com.huizu.project.domain.SysUserAuth;
import com.huizu.project.service.IProXxService;
import com.huizu.project.service.ISysUserAuthService;
import com.huizu.project.service.impl.ProXxServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huizu.common.annotation.Log;
import com.huizu.common.core.controller.BaseController;
import com.huizu.common.core.domain.AjaxResult;
import com.huizu.common.enums.BusinessType;
import com.huizu.project.domain.ProUser;
import com.huizu.project.service.IProUserService;
import com.huizu.common.utils.poi.ExcelUtil;
import com.huizu.common.core.page.TableDataInfo;

/**
 * 用户管理Controller
 * 
 * @author zhangmx
 * @date 2024-05-10
 */
@RestController
@RequestMapping("/project/prouser")
public class ProUserController extends BaseController
{
    @Autowired
    private IProUserService proUserService;

    @Autowired
    private ISysUserAuthService iSysUserAuthService;

    @Autowired
    private IProXxService iProXxService;

    /**
     * 查询用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('project:prouser:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProUser proUser)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!SecurityUtils.isAdmin(user.getUserId())){
            SysUserAuth sysUserAuthPram = new SysUserAuth();
            sysUserAuthPram.setUserId(user.getUserId());
            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
            List<String> bjs = auths.stream().filter(auth->auth.getType().equals("bj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
            proUser.setBjIds(bjs);
        }
        startPage();
        List<ProUser> list = proUserService.selectProUserList(proUser);
        return getDataTable(list);
    }

    /**
     * 查询用户管理列表
     */
    //@PreAuthorize("@ss.hasPermi('project:prouser:list')")
    @GetMapping("/listTotal")
    public TableDataInfo listTotal(ProUser proUser)
    {
        List<ProUser> list = proUserService.selectProUserList(proUser);
        return getDataTable(list);
    }

    /**
     * 导出用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('project:prouser:export')")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProUser proUser)
    {
        List<ProUser> list = proUserService.selectProUserList(proUser);
        ExcelUtil<ProUser> util = new ExcelUtil<ProUser>(ProUser.class);
        util.exportExcel(response, list, "用户管理数据");
    }

    /**
     * 获取用户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:prouser:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") String userId)
    {
        return success(proUserService.selectProUserByUserId(userId));
    }

    /**
     * 新增用户管理
     */
    @PreAuthorize("@ss.hasPermi('project:prouser:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProUser proUser) throws ServerException
    {
        List<ProXx>  proXys = iProXxService.selectProXxList(new ProXx());
        if (CollUtil.isEmpty(proXys)){
            throw new ServerException("学校积分上限未设置");
        }
        proUser.setXxId(proXys.get(0).getId());
        ProUser user = new ProUser();
        user.setStNumber(proUser.getStNumber());
        List<ProUser> list = proUserService.selectProUserList(user);
        if (CollUtil.isNotEmpty(list)){
            throw new ServiceException("对不起，您输入的学号重复[" +proUser.getStNumber() + "]");
        }
        return toAjax(proUserService.insertProUser(proUser));
    }

    /**
     * 修改用户管理
     */
    //@PreAuthorize("@ss.hasPermi('project:prouser:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProUser proUser)
    {
        ProUser user = new ProUser();
        user.setStNumber(proUser.getStNumber());
        List<ProUser> list = proUserService.selectProUserByNumberNotUserId(user);
        if (CollUtil.isNotEmpty(list)){
            throw new ServiceException("对不起，您输入的学号重复[" +proUser.getStNumber() + "]");
        }
        return toAjax(proUserService.updateProUser(proUser));
    }

    /**
     * 修改用户管理
     */
    @PreAuthorize("@ss.hasPermi('project:prouser:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPassword")
    public AjaxResult resetPassword(@RequestBody ProUser proUser)
    {
        return toAjax(proUserService.resetPassword(proUser));
    }

    /**
     * 删除用户管理
     */
    @PreAuthorize("@ss.hasPermi('project:prouser:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable String[] userIds)
    {
        return toAjax(proUserService.deleteProUserByUserIds(userIds));
    }

    @PostMapping("/getCountCjByUser")
    public AjaxResult getCountCjByUser(@RequestBody Map<String,Object> map, HttpServletRequest request){
        return proUserService.getCountCj(map);
    }
}
